package com.example.mywanandroid.util;
import java.io.File;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class HttpUtil {
    public static void sendOkHttpRequest(String address, okhttp3.Callback callback) {
        // 配置缓存
        int cacheSize = 10 * 1024 * 1024; // 10MB
        //缓存文件夹
        File cacheFile = new File("/storage/emulated/0/Android/data/com.example.mywanandroid/cache", "a_cache");

        // 需要指定一个私有目录，以及缓存限制的大小，官方给出的限制是10MB
        Cache cache = new Cache(cacheFile, cacheSize);
        CacheControl cacheControl = new CacheControl.Builder()
                .maxStale(5, TimeUnit.SECONDS)
                .build();
        OkHttpClient client = new OkHttpClient.Builder().cache(cache).build();
        Request request = new Request.Builder().url(address)
                .cacheControl(cacheControl)
                .build();
        client.newCall(request).enqueue(callback);
    }
}
